package cn.dianhun.om.jpa.pojo;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author cyt
 * @create 2020-07-08 10:42
 */

@Accessors(chain = true)
@Data
@Entity
@Table(name = "sys_user", schema = "renren", catalog = "")
public class SysUserEntity {
    @Id
    @Column(name = "user_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long userId;
    private String username;
    private String password;
    private String salt;
    private String email;
    private String mobile;
    private Byte status;
    private Long createUserId;

    // 自动填充时间
    @CreatedDate
    private Timestamp createTime;

    /**
     * 中间表名
     * <p>
     * 当前对象在中间表的外键
     * 对方在中间表的外键
     */
    @JoinTable(name = "sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "role_id")})
    @ManyToMany(targetEntity = SysRoleEntity.class, cascade = CascadeType.ALL)
    private Set<SysRoleEntity> roleEntities = new HashSet<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SysUserEntity that = (SysUserEntity) o;
        return userId == that.userId &&
                Objects.equals(username, that.username) &&
                Objects.equals(password, that.password) &&
                Objects.equals(salt, that.salt) &&
                Objects.equals(email, that.email) &&
                Objects.equals(mobile, that.mobile) &&
                Objects.equals(status, that.status) &&
                Objects.equals(createUserId, that.createUserId) &&
                Objects.equals(createTime, that.createTime);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, username, password, salt, email, mobile, status, createUserId, createTime);
    }
}
